<template>
  <div class="count-flop">
    <div v-for="(item, idx) in data" :key="idx" class="row flex-center fit">
      <div class="digital-flop-icon flex flex-center">
        <q-icon :name="item.icon" size="60px" color="light-blue" />
      </div>
      <div class="digital-flop-title">{{ item.title }}</div>
      <div class="digital-flop">
        <count-to
          :start-val="oldNumber(idx)"
          :end-val="item.number || 0"
          :duration="3000"
          :style="item.style || null"
        />
        <div class="unit">{{ item.unit }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref } from 'vue';
import { CountTo } from 'vue3-count-to';

export default defineComponent({
  name: 'CountFlop',
  props: {
    data: {
      type: [Array, Object],
      default: () => { }
    }
  },
  components: {
    CountTo
  },
  data () {
    return {
      oldData: null
    }
  },
  mounted () {
  },
  methods: {
    oldNumber (idx) {
      if (this.oldData && this.oldData[idx] && this.oldData[idx].config && this.oldData[idx].number) {
        return this.oldData[idx].number || 0;
      } else {
        return 0;
      }
    }
  },
  watch: {
    data: {
      handler (newVal, oldVal) {
        this.oldData = newVal;
      },
      deep: true,
      immediate: true
    }
  },
  computed: {
  }
});
</script>

<style lang="scss">
.count-flop {
  position: relative;
  height: 150px;
  flex-shrink: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .digital-flop-icon {
    border: 4px solid $light-blue;
    border-radius: 50%;
    box-shadow: 0 0 20px rgba(141, 152, 253, 0.8), inset 0 0 20px rgba(141, 152, 253, 0.6);
    height: 90px;
    width: 90px;
  }

  .digital-flop-title {
    text-align: center;
    font-size: 30px;
    padding: 10px 20px;
    color: $light-blue-2;
  }

  .digital-flop {
    display: flex;
    justify-content: center;
  }
  .unit {
    margin-left: 10px;
    display: flex;
    align-items: flex-end;
    box-sizing: border-box;
    padding-bottom: 24px;
    font-size: 26px;
    color: $light-blue-2;
  }
}
</style>
